;
(function (window, angular) {
	'use strict';
	angular.module("Controller.BasicData.WjReportProModel", [])
		.controller("WjReportProModelCtrl", ["$scope", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
			$scope.markInfo = {
				allChecked: false,
				curRow: -1
			};
			$scope.pageInfo = {
				totalPages: 1,
				totalRows: 0,
				pageSize: 10,
				curPage: 1
			};
			$scope.toolbarConfig = {
				showColConfig: false
			};


			$scope.condition = {
				dbdFlag:"1"
			};
			$scope.dataList = [];
			$scope.tableConfig = {
				fixedWidth: 100,
				scrollWidth: 1400,
				tableCol: [
					{
						name: "编码",
						dataField: "code",
						width: 120,
						isShow: true,
						isFixed: false,
					},
					{
						name: "名称",
						dataField: "name",
						width: 120,
						isShow: true,
						isFixed: false,
					},
					{
						name: "产品线",
						dataField: "productLine",
						width: 120,
						isShow: true,
						isFixed: false,
					},
					{
						name: "产品类别",
						dataField: "productCategory",
						width: 120,
						isShow: true,
						isFixed: false,
					},
					{
						name: "创建时间",
						dataField: "createdTime",
						width: 120,
						isShow: true,
						isFixed: false,
					}
				]
			};
			$scope.setFixedWidth = function () {
				$scope.tbStyle = {
					scrollLeft: { left: $scope.tableConfig.fixedWidth + "px" }
				};
			};

			$scope.exportParam = {
				sectionParam: { ids: [] },
				allUrl: ybHttp.RequestUrl("device", "/wjReportProModel/exportAll")
			};

			$scope.reset = function () {
				$scope.condition = {
					dbdFlag:'1'
				};
			};

			function getDataList(curPage) {
				$scope.markInfo.allChecked = false;
				$scope.markInfo.curRow = -1;
				$scope.exportParam.sectionParam.ids = [];

				var service = "device";
				var reqUrl = "/wjReportProModel/list";

				if (!!curPage) {
					$scope.pageInfo.curPage = curPage;
				}

				var param = {
					pageSize: $scope.pageInfo.pageSize,
					pageNum: $scope.pageInfo.curPage,
					filters: angular.copy($scope.condition)
				};
				ybHttp.Post(service, reqUrl, param).then(function (resp) {
					if (resp.code !== 0) {
						ybWidget.ShowMessage(resp, MESG_TYPE.Error);
						return;
					}
					angular.forEach(resp.value.list, function (item) {
						item.isChecked = false;
						item.createdTime = $filter("YbDate")(item.createdTime, 'yyyy-MM-dd HH:mm:ss');
					});
					$scope.pageInfo.totalRows = resp.value.total;
					$scope.pageInfo.totalPages = resp.value.pages;
					$scope.dataList = resp.value.list;
				});
			}


			$scope.switchToolbar = function (toolbar) {
				if (!!$scope.toolbarConfig["show" + toolbar]) {
					$scope.toolbarConfig["show" + toolbar] = false;
				} else {
					$timeout(function () {
						$scope.toolbarConfig["show" + toolbar] = true;
					});
				}
			};

			$scope.selectRow = function (index) {
				$scope.markInfo.curRow = index
			};

			$scope.search = function (curpage) {
				getDataList(curpage);
			};

			function getBillIds() {
				$scope.exportParam.sectionParam.ids = [];
				angular.forEach($scope.dataList, function (item) {
					if (!!item.isChecked) {
						$scope.exportParam.sectionParam.ids.push(item.id);
					}
				});
			}

			$scope.selectAll = function (list, type) {
				for (var i = 0, len = list.length; i < len; i++) {
					list[i].isChecked = angular.isUndefined(type) ? $scope.markInfo.allChecked : $scope.markInfo["allChecked" + type];
				}
				getBillIds();
			};

			function checkAll(list) {
				var selectAll = false;
				for (var i = 0, len = list.length; i < len; i++) {
					if (!list[i].isChecked) {
						selectAll = false;
						break;
					}
					selectAll = true;
				}
				return selectAll;
			}

			$scope.selectSingle = function (item, list, type) {
				var selectAll = false;
				if (!item.isChecked) {
					selectAll = false;
				} else {
					if (!checkAll(list)) {
						selectAll = false;
					} else {
						selectAll = true;
					}
				}
				if (angular.isDefined(type)) {
					$scope.markInfo["allChecked" + type] = selectAll;
				} else {
					$scope.markInfo.allChecked = selectAll;
				}
				getBillIds();
			};

			(function () {
				$scope.setFixedWidth();
				getDataList();
			})();
		}])
		;
})(window, window.angular);
